# Maintainer: Maciej Suminski <maciej.suminski@cern.ch>
#
# Based on Arch ngspice PKGBUILD:
# https://git.archlinux.org/svntogit/community.git/tree/trunk/PKGBUILD?h=packages/ngspice
#   Contributor: Kyle Keen <keenerd@gmail.com>
#   Contributor: Abhishek Dasgupta <abhidg@gmail.com>
#   Contributor: Jason Taylor <jftaylor21@gmail.com>
#   Contributor: Luis Henrique <lmello.009@gmail.com>

_realname=ngspice
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=33
pkgrel=2
pkgdesc="Mixed-level/Mixed-signal circuit simulator based on Spice3f5, Cider1b1, and Xspice (mingw-w64)"
url='https://ngspice.sourceforge.io/'
license=('BSD')
arch=('any')
groups=("${MINGW_PACKAGE_PREFIX}-eda")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
source=("https://downloads.sourceforge.net/project/${_realname}/ng-spice-rework/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "https://downloads.sourceforge.net/project/${_realname}/ng-spice-rework/${pkgver}/${_realname}-doc-${pkgver}.tar.gz")
sha256sums=('b99db66cc1c57c44e9af1ef6ccb1dcbc8ae1df3e35acf570af578f606f8541f1'
            '1f90d3bdad11eee66079b086e8fd54db5bac7b7eb183a9afbb573ec351cedfb0')

build() {
  [[ -d "${srcdir}/build-shared-${CARCH}" ]] && rm -rf "${srcdir}/build-shared-${CARCH}"
  mkdir -p "${srcdir}/build-shared-${CARCH}" && cd "${srcdir}/build-shared-${CARCH}"

  # FS#45230, create so lib
  # shared lib sets flags and modifies headers, needs dedicated pass
  # adding --with-readline disables libngspice-0.dll
  ../${_realname}-${pkgver}/configure \
              --prefix=${MINGW_PREFIX} \
              --build=${MINGW_CHOST} \
              --host=${MINGW_CHOST} \
              --enable-openmp \
              --enable-xspice \
              --enable-cider \
              --with-ngshared
  make

  cd "${srcdir}/${_realname}-${pkgver}"
  [[ -d "${srcdir}/build-static-${CARCH}" ]] && rm -rf "${srcdir}/build-static-${CARCH}"
  mkdir -p "${srcdir}/build-static-${CARCH}" && cd "${srcdir}/build-static-${CARCH}"

  ../${_realname}-${pkgver}/configure \
              --prefix=${MINGW_PREFIX} \
              --build=${MINGW_CHOST} \
              --host=${MINGW_CHOST} \
              --enable-xspice \
              --enable-cider \
              --enable-openmp
  make
}

package() {
  cd "${srcdir}/build-static-${CARCH}"
  make install DESTDIR="${pkgdir}"
  install -D -m644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  # pkg-conf file has to be copied manually.  Generated pkg-config file is the same for
  # static and shared builds.
  install -D -m644 "${srcdir}/build-shared-${CARCH}/ngspice.pc" "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/ngspice.pc"

  # library files have to be copied manually
  cd "${srcdir}/build-shared-${CARCH}"
  install -D -m755 src/.libs/libngspice-0.dll "${pkgdir}${MINGW_PREFIX}/bin"
  install -D -m755 src/.libs/libngspice.dll.a "${pkgdir}${MINGW_PREFIX}/lib"
}
